import { _decorator, Component, find, instantiate, Node, NodePool, Prefab } from 'cc';
import PieceInit from './PieceInit';
import { PieceManagerBase } from './PieceManagerBase';
import { QipanController } from './QipanController';
import { NodeController } from './NodeController';
import { MoveOrEatManager } from './MoveOrEatManager';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    @property(Prefab)
    bingRed: Prefab = null;
    @property(Prefab)
    bingBlack: Prefab = null;
    @property(Prefab)
    maRed: Prefab = null;
    @property(Prefab)
    maBlack: Prefab = null;
    @property(Prefab)
    paoRed: Prefab = null;
    @property(Prefab)
    paoBlack: Prefab = null;
    @property(Prefab)
    shiRed: Prefab = null;
    @property(Prefab)
    shiBlack: Prefab = null;
    @property(Prefab)
    bossRed: Prefab = null;
    @property(Prefab)
    bossBlack: Prefab = null;
    @property(Prefab)
    cheRed: Prefab = null;
    @property(Prefab)
    cheBlack: Prefab = null;
    @property(Prefab)
    xiangRed: Prefab = null;
    @property(Prefab)
    xiangBlack: Prefab = null;
    @property(Prefab)
    qipan: Prefab = null;
    @property(Prefab)
    allowNode: Prefab;
    @property(Prefab)
    attackNode: Prefab;
    allowNodePool: Node[] = [] ;
    attckNodePool: Node[] = [] ;
    currentPiece: PieceManagerBase = null;
    moveList: Node[] = [];
    attackList: Node[] = [];
    private static instance: GameManager = null;
    isPlayerRed: boolean = false;
    redPiece: Node[] = [];
    blackPiece: Node[] = [];
    qipanController: QipanController;
    piecesNode: Node;
    public static getInstance(): GameManager {
        if (!this.instance) {
            this.instance = new GameManager();
        }
        return this.instance;
    }
    initPiece() {
        return new Promise((resolve, reject) => {
            PieceInit.getBlackInit().forEach(element => {
                let node = instantiate(this[element.prefabName]);
                let pieceBase = node.getComponent(PieceManagerBase);
                if (this.isPlayerRed) {
                    element.posX = 8 - element.posX;
                    element.posY = 9 - element.posY;
                    pieceBase.isEnemy = true;
                }
                pieceBase.posX = element.posX;
                pieceBase.posY = element.posY;
                pieceBase.pieceName = element.name;
                pieceBase.initPiece();
                this.piecesNode.addChild(node);
                this.redPiece.push(node);
                // console.log(123,this.redPiece);

            });
            PieceInit.getRedInit().forEach(element => {
                let node = instantiate(this[element.prefabName]);
                const pieceBase = node.getComponent(PieceManagerBase);
                if (!this.isPlayerRed) {
                    element.posX = 8 - element.posX;
                    element.posY = 9 - element.posY;
                    pieceBase.isEnemy = true;
                }
                pieceBase.posX = element.posX;
                pieceBase.posY = element.posY;
                pieceBase.pieceName = element.name;
                pieceBase.initPiece();
                this.piecesNode.addChild(node);
                this.blackPiece.push(node);
            });
            resolve(true);
        })
    }
    initQipan() {
        return new Promise((resolve, reject) => {
            let qipan = instantiate(this.qipan);
            this.node.parent.insertChild(qipan, 0);

            resolve(true);
        })
    }
    getAllowNode() {
        // console.log(this.allowNodePool);
        
        if (this.allowNodePool.length < 0) {
            return instantiate(this.allowNode);
        } else {
            let node = this.allowNodePool.pop();
            
            return node;
        }
    }
    getAttackNode() {
        if (this.attckNodePool.length < 0) {
            return instantiate(this.attackNode);
        } else {
            return this.attckNodePool.pop();
        }
    }
    putAllowNode(node: Node) {
        node.active = false;
        this.allowNodePool.push(node);
    }
    putAttackNode(node: Node) {
        node.active = false;
        this.attckNodePool.push(node);
    }

    clearMoveOrEatNode() {
        this.currentPiece = null;
        for (let index = 0; index < this.moveList.length; index++) {
            const element = this.moveList[index];
            element.getComponent(MoveOrEatManager).targetNode = null;
            this.putAllowNode(element);
        }
        for (let index = 0; index < this.attackList.length; index++) {
            const element = this.attackList[index];
            element.getComponent(MoveOrEatManager).targetNode = null;
            this.putAttackNode(element);
        }
        this.attackList = [];
        this.moveList = [];
    }
    async start() {
        this.piecesNode = this.node.parent.getChildByName("Pieces");
        await this.initQipan()
        await this.initPiece()
        // this.qipanController = QipanController.getInstance();
        for (let i = 0; i < 20; i++) {
            let allowNode = instantiate(this.allowNode);
            let attackNode = instantiate(this.attackNode);
            allowNode.active = false;
            attackNode.active = false;
            this.allowNodePool.push(allowNode);
            this.attckNodePool.push(attackNode);
            this.piecesNode.addChild(allowNode);
            this.piecesNode.addChild(attackNode);
        }
    }

    update(deltaTime: number) {

    }
}

